package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.ModuleDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.ModuleService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName ModuleController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 12:22:48
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/module")
public class ModuleController {

    private static final Logger LOG = LoggerFactory.getLogger(ModuleController.class);
    public static final String BUSINESS_NAME = "功能模块表";

    @Resource
    private ModuleService moduleService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        moduleService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody ModuleDto moduleDto) {
        // 保存校验
        ValidatorUtil.require(moduleDto.getId(), "功能模块流水号");
        ValidatorUtil.length(moduleDto.getId(), "功能模块流水号", 1, 50);
        ValidatorUtil.require(moduleDto.getCascadeid(), "节点语义ID");
        ValidatorUtil.length(moduleDto.getCascadeid(), "节点语义ID", 1, 255);
        ValidatorUtil.require(moduleDto.getName(), "功能模块名称");
        ValidatorUtil.length(moduleDto.getName(), "功能模块名称", 1, 255);
        ValidatorUtil.require(moduleDto.getUrl(), "主页面URL");
        ValidatorUtil.length(moduleDto.getUrl(), "主页面URL", 1, 255);
        ValidatorUtil.require(moduleDto.getHotkey(), "热键");
        ValidatorUtil.length(moduleDto.getHotkey(), "热键", 1, 255);
        ValidatorUtil.require(moduleDto.getIsleaf(), "是否叶子节点");
        ValidatorUtil.require(moduleDto.getIsautoexpand(), "是否自动展开");
        ValidatorUtil.require(moduleDto.getIconname(), "节点图标文件名称");
        ValidatorUtil.length(moduleDto.getIconname(), "节点图标文件名称", 1, 255);
        ValidatorUtil.require(moduleDto.getStatus(), "当前状态");
        ValidatorUtil.require(moduleDto.getParentname(), "父节点名称");
        ValidatorUtil.length(moduleDto.getParentname(), "父节点名称", 1, 255);
        ValidatorUtil.require(moduleDto.getVector(), "矢量图标");
        ValidatorUtil.length(moduleDto.getVector(), "矢量图标", 1, 255);
        ValidatorUtil.require(moduleDto.getSortno(), "排序号");
        ValidatorUtil.length(moduleDto.getParentid(), "父节点流水号", 1, 50);
        ValidatorUtil.length(moduleDto.getCode(), "", 1, 50);
        ValidatorUtil.require(moduleDto.getIssys(), "是否为系统模块");

        ResponseDto responseDto = new ResponseDto();
        moduleService.save(moduleDto);
        responseDto.setContent(moduleDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        moduleService.delete(id);
        return responseDto;
    }
}
